package com.orangelife.service.propFeeRuleParam.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

import com.github.pagehelper.PageHelper;
import com.orangelife.common.MybaitsGenericDaoSubLogic;
import com.orangelife.exception.AppBizException;
import com.orangelife.service.propFeeRuleParam.IPropFeeRuleParamService;
import com.orangelife.service.propFeeRuleParam.model.PropFeeRuleParamDto;
import com.orangelife.setting.constant.CodeConst;
import com.orangelife.setting.constant.InfoConst;
import com.orangelife.setting.constant.TypeConst;
import com.orangelife.utils.IdUtil;
import com.orangelife.utils.StringUtils;

/*****************************************************************
 * Copyright(C) ORANGELIFE Corporation CodeConst.BIS_CODE_68001015. All Rights
 * Reserved.
 *****************************************************************/
/*
 * ============================================================== 功能名
 * PropFeeRuleParam类的 service层的增删改查
 * 
 * @author @author zhr
 * 
 * @version 1.1.0
 * 
 * @date 2016-07-25 10:43:27
 * ==============================================================
 */
@Service("propFeeRuleParamService")
public class PropFeeRuleParamServiceImpl extends MybaitsGenericDaoSubLogic
		implements IPropFeeRuleParamService {
	private static Logger logger = Logger
			.getLogger(PropFeeRuleParamServiceImpl.class);

	public static Logger getLogger() {
		return logger;
	}

	public static void setLogger(Logger logger) {
		PropFeeRuleParamServiceImpl.logger = logger;
	}

	@Override
	@Transactional(rollbackFor = { AppBizException.class, Exception.class,
			RuntimeException.class })
	public int addPropFeeRuleParam(PropFeeRuleParamDto propFeeRuleParam)
			throws AppBizException {

		Assert.notNull(propFeeRuleParam);
		/************************************************
		 * 判断输入参数的长度以及是否能为空
		 ************************************************/

		if (!StringUtils.isBlank(propFeeRuleParam.getParamCode())) {
			if (propFeeRuleParam.getParamCode().length() > 16) {
				throw new AppBizException(CodeConst.SYS_CODE_1002, "参数编码长度超出");
			}
		}

		if (!StringUtils.isBlank(propFeeRuleParam.getParamType())) {
			if (propFeeRuleParam.getParamType().length() > 1) {
				throw new AppBizException(CodeConst.SYS_CODE_1002,
						"参数类型(1固定值，2前端输入)长度超出");
			}
		}

		if (!StringUtils.isBlank(propFeeRuleParam.getParamVal())) {
			if (propFeeRuleParam.getParamVal().length() > 2000) {
				throw new AppBizException(CodeConst.SYS_CODE_1002, "参数值长度超出");
			}
		}

		propFeeRuleParam.setParamID(IdUtil.getuuId());
		propFeeRuleParam.setItemStat(TypeConst.FLAG_ON);
		propFeeRuleParam.setTscreate(new Date());
		propFeeRuleParam.setTsedit(new Date());
		return this.getSqlSession().insert(
				"PropFeeRuleParamMapper.insertSelective", propFeeRuleParam);
	}

	@Override
	@Transactional(rollbackFor = { AppBizException.class, Exception.class,
			RuntimeException.class })
	public int editPropFeeRuleParam(PropFeeRuleParamDto propFeeRuleParam)
			throws AppBizException {

		Assert.notNull(propFeeRuleParam);
		/************************************************
		 * 判断输入参数的长度以及是否能为空
		 ************************************************/
		if (StringUtils.isBlank(propFeeRuleParam.getParamID())) {
			throw new AppBizException(CodeConst.SYS_CODE_1001, "ID不可为空");
		}
		PropFeeRuleParamDto oldPropFeeRuleParam = this
				.findPropFeeRuleParamByID(propFeeRuleParam.getParamID());

		if (!StringUtils.isBlank(propFeeRuleParam.getParamCode())) {
			if (propFeeRuleParam.getParamCode().length() > 16) {
				throw new AppBizException(CodeConst.SYS_CODE_1002, "参数编码长度超出");
			}
		}

		if (!StringUtils.isBlank(propFeeRuleParam.getParamType())) {
			if (propFeeRuleParam.getParamType().length() > 1) {
				throw new AppBizException(CodeConst.SYS_CODE_1002,
						"参数类型(1固定值，2前端输入)长度超出");
			}
		}

		if (!StringUtils.isBlank(propFeeRuleParam.getParamVal())) {
			if (propFeeRuleParam.getParamVal().length() > 2000) {
				throw new AppBizException(CodeConst.SYS_CODE_1002, "参数值长度超出");
			}
		}

		if (oldPropFeeRuleParam == null) {
			throw new AppBizException(CodeConst.BIS_CODE_68001,
					InfoConst.BIS_INFO_68001);
		}
		propFeeRuleParam.setTsedit(new Date());
		return this.getSqlSession().update(
				"PropFeeRuleParamMapper.updateByPrimaryKeySelective",
				propFeeRuleParam);
	}

	@Override
	public PropFeeRuleParamDto findPropFeeRuleParamByID(
			String propFeeRuleParamID) throws AppBizException {
		if (StringUtils.isBlank(propFeeRuleParamID)) {

			throw new AppBizException(CodeConst.SYS_CODE_1001, "ID不可为空");
		}

		/************************************************
		 * 判断实体是否存在
		 ************************************************/
		PropFeeRuleParamDto oldPropFeeRuleParam = this.getSqlSession()
				.selectOne("PropFeeRuleParamSearchMapper.selectByPrimaryKey",
						propFeeRuleParamID);
		if (oldPropFeeRuleParam == null) {
			throw new AppBizException(CodeConst.BIS_CODE_68001,
					InfoConst.BIS_INFO_68001);
		}

		if (TypeConst.FLAG_OFF.equals(oldPropFeeRuleParam.getItemStat())) {

			throw new AppBizException(CodeConst.BIS_CODE_68001,
					InfoConst.BIS_INFO_68001);
		}
		return oldPropFeeRuleParam;
	}

	@Override
	@Transactional(rollbackFor = { AppBizException.class, Exception.class,
			RuntimeException.class })
	public int deletePropFeeRuleParamByLogic(String propFeeRuleParamID)
			throws AppBizException {

		if (StringUtils.isBlank(propFeeRuleParamID)) {
			throw new AppBizException(CodeConst.SYS_CODE_1001, "ID不可为空");
		}
		/************************************************
		 * 判断实体是否存在
		 ************************************************/
		PropFeeRuleParamDto oldPropFeeRuleParam = this
				.findPropFeeRuleParamByID(propFeeRuleParamID);

		oldPropFeeRuleParam.setItemStat(TypeConst.FLAG_OFF);
		oldPropFeeRuleParam.setTsedit(new Date());
		return this.getSqlSession().update(
				"PropFeeRuleParamMapper.updateByPrimaryKeySelective",
				oldPropFeeRuleParam);
	}

	@Override
	public List<Map<String, Object>> findPropFeeRuleParamByPage(String type,
			String commID, String tsCreate, String search, int pageNo,
			int pageSize) throws AppBizException {

		/************************************************
		 * 设置输入参数
		 ************************************************/
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("tsCreate", tsCreate);
		map.put("commID", commID);
		map.put("type", type);
		map.put("search", search);
		PageHelper.startPage(pageNo, pageSize);
		List<Map<String, Object>> list = this.getSqlSession().selectList(
				"PropFeeRuleParamSearchMapper.findAllPropFeeRuleParam", map);
		return list;
	}

	@Override
	public List<PropFeeRuleParamDto> getPropFeeRuleParamByPage(String tsCreate,
			String search, int pageNo, int pageSize) throws AppBizException {
		/************************************************
		 * 设置输入参数
		 ************************************************/
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("tsCreate", tsCreate);
		map.put("search", search);
		PageHelper.startPage(pageNo, pageSize);
		List<PropFeeRuleParamDto> list = this.getSqlSession().selectList(
				"PropFeeRuleParamSearchMapper.getAllPropFeeRuleParamDto", map);
		return list;
	}

	@Override
	public List<Map<String, Object>> selectPropFeeRuleParamByRelevantID(
			String ruleID, String paramID, String paramType)
			throws AppBizException {
		/************************************************
		 * 设置输入参数
		 ************************************************/
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("paramID", paramID);
		map.put("paramType", paramType);
		map.put("ruleID", ruleID);

		List<Map<String, Object>> list = this
				.getSqlSession()
				.selectList(
						"PropFeeRuleParamSearchMapper.selectPropFeeRuleParamDtoyRelevantID",
						map);
		return list;
	}

	@Override
	public List<PropFeeRuleParamDto> findPropFeeRuleParamByRelevantID(
			String ruleID, String paramID, String paramType)
			throws AppBizException {
		/************************************************
		 * 设置输入参数
		 ************************************************/
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("paramID", paramID);
		map.put("paramType", paramType);
		map.put("ruleID", ruleID);
		List<PropFeeRuleParamDto> list = this
				.getSqlSession()
				.selectList(
						"PropFeeRuleParamSearchMapper.findPropFeeRuleParamDtoByRelevantID",
						map);
		return list;
	}

	@Override
	public List<PropFeeRuleParamDto> bauthfindPropFeeRuleParam(
			List<String> propFeeRuleParamIDs) throws AppBizException {
		/************************************************
		 * 设置输入参数
		 ************************************************/
		List<PropFeeRuleParamDto> list = this.getSqlSession().selectList(
				"PropFeeRuleParamSearchMapper.batchFindPropFeeRuleParam",
				propFeeRuleParamIDs);
		return list;
	}

	@Override
	public List<Map<String, Object>> bauthfindPropFeeRuleParamByMap(
			Map<String, Object> propFeeRuleParam) throws AppBizException {
		/************************************************
		 * 设置输入参数
		 ************************************************/
		List<Map<String, Object>> list = this.getSqlSession().selectList(
				"PropFeeRuleParamSearchMapper.batchFindPropFeeRuleParamByMap",
				propFeeRuleParam);
		return list;
	}

	@Override
	public void clearCache() throws AppBizException {
		System.out.println("清除缓存成功");
	}

}